package com.datacloud.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;
import com.datacloud.bean.TiresBean;
import com.datacloud.bean.TireEventsBean;
import com.datacloud.bean.TiresInfo;
import com.datacloud.bean.VehiclesBean;
import com.datacloud.service.CarsManageService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 创建人   caizhongrui
 * 创建时间  下午10:29
 * ==============================================
 * 类说明：
 * <p>
 * ==============================================
 * 修改记录
 * 修改人          修改时间             修改内容
 * 蔡仲瑞          下午10:29             创建
 * ==============================================
 */
@Controller
@RequestMapping("cars")
public class CarsManageController {

	
	@Autowired
    CarsManageService carsManageService;
	 //fastjson中对日期的序列化定义
    private static SerializeConfig serializeConfig = new SerializeConfig();
    private static String dateFormat;
    static {
        dateFormat = "yyyy-MM-dd HH:mm:ss";
        serializeConfig.put(Date.class, new SimpleDateFormatSerializer(dateFormat));
    }
    /**
     * 获取未绑定车列表
     * @return
     */
    @RequestMapping(value = "/getNoBindingCars", method = RequestMethod.GET, produces = "application/json; charset=gbk")
    @ResponseBody
    public String getNoBindingCars(){
        
        /*Map<String,Object> mapAll=new HashMap<String, Object>();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("vehicles", carsManageService.getNoBindingCars());
        mapAll.put("success","success");
        mapAll.put("data",carsManageService.getNoBindingCars());
        mapAll.put("msg","error msg");
        String data = "{'data':[{'brand':'111'}]}";
        JSONObject jsonA = JSONObject.parseObject(data);
        String tabInfo = jsonA.getString("data");
        JSONArray jsons = JSONArray.parseArray(tabInfo);
        JSONObject jsonOBJ = jsons.getJSONObject(0);
        VehiclesBean ve = JSONObject.toJavaObject(jsonOBJ, VehiclesBean.class);
    	return JSONObject.toJSONString(mapAll,serializeConfig);*/
    	JSONObject result = new JSONObject();
        result.put("success",true);
        result.put("msg","");
        result.put("data",carsManageService.getNoBindingCars());
        return JSON.toJSONString(result,serializeConfig);
    }
    /**
     * 进行车辆绑定
     * @param //type
     * @param //msg
     * @param //source
     * @return
     */
    @RequestMapping(value = "/binding", method = RequestMethod.GET, produces = "application/json; charset=gbk")
    @ResponseBody
    public String sendMsg(@RequestParam(value = "carid", required = true) int carid,@RequestParam(value = "epc", required = true) String epc){
        JSONObject result = new JSONObject();
        result.put("success",carsManageService.binding(carid, epc));
        result.put("msg","");
        return result.toJSONString();
    }
    /**
     * 获取所有路线
     * @return
     */
    @RequestMapping(value = "/findAllRoutes", method = RequestMethod.GET, produces = "application/json; charset=gbk")
    @ResponseBody
    public String findAllRoutes(){
        JSONObject result = new JSONObject();
        result.put("success",true);
        result.put("msg","");
        result.put("data",carsManageService.findAllRoutes());
        return JSON.toJSONString(result,serializeConfig);
    }
    /**
     * 根据epc获取车信息
     * @return
     */
    @RequestMapping(value = "/findVehiclesByEpc", method = RequestMethod.GET, produces = "application/json; charset=gbk")
    @ResponseBody
    public String findVehiclesByEpc(@RequestParam(value = "epc", required = true) String epc){
        List<VehiclesBean> infos = new ArrayList<VehiclesBean>();
        VehiclesBean info = carsManageService.findVehiclesByEpc(epc);
        infos.add(info);
    	JSONObject result = new JSONObject();
        result.put("success",true);
        result.put("data",infos);
        result.put("msg","");
    	return JSONObject.toJSONString(result,serializeConfig);
    }
    /**
     * 获取所有轮胎品牌
     */
    @RequestMapping(value = "/findAllTireBrands", method = RequestMethod.GET, produces = "application/json; charset=gbk")
    @ResponseBody
    public String findAllTireBrands(){
        JSONObject result = new JSONObject();
        result.put("success",true);
        result.put("data",carsManageService.findAllTireBrands());
        result.put("msg","");
    	return JSONObject.toJSONString(result,serializeConfig);
    }
    /**
     * 获取所有轮胎规格
     */
    @RequestMapping(value = "/findAllTireModels", method = RequestMethod.GET, produces = "application/json; charset=gbk")
    @ResponseBody
    public String findAllTireModels(){
        JSONObject result = new JSONObject();
        result.put("success",true);
        result.put("data",carsManageService.findAllTireModels());
        result.put("msg","");
    	return JSONObject.toJSONString(result,serializeConfig);
    }
    /**
     * 获取所有轮胎花纹
     */
    @RequestMapping(value = "/findAllTirePatterns", method = RequestMethod.GET, produces = "application/json; charset=gbk")
    @ResponseBody
    public String findAllTirePatterns(){
        JSONObject result = new JSONObject();
        result.put("success",true);
        result.put("data",carsManageService.findAllTirePatterns());
        result.put("msg","");
    	return JSONObject.toJSONString(result,serializeConfig);
    }
    /**
     * 获取轮胎信息
     */
    @RequestMapping(value = "/findTiresInfo", method = RequestMethod.GET, produces = "application/json; charset=gbk")
    @ResponseBody
    public String findTiresInfo(@RequestParam("vehicleid") String vehicleid,@RequestParam("wheelaliasid") String wheelaliasid,@RequestParam("epc") String epc){
        List<TiresInfo> infos = new ArrayList<TiresInfo>();
        TiresInfo info = carsManageService.findTiresInfo(epc, vehicleid, wheelaliasid);
        infos.add(info);
    	JSONObject result = new JSONObject();
        result.put("success",true);
        result.put("data",infos);
        result.put("msg","");
    	return JSONObject.toJSONString(result,serializeConfig);
    }
    /**
     * 进行轮胎绑定
     * @param type
     * @param /msg
     * @param /source
     * @return
     */
    @RequestMapping(value = "/tireBinding", method = RequestMethod.POST, produces = "application/json; charset=gbk")
    @ResponseBody
    public String sendMsg(@RequestParam("miles") int miles,@RequestParam("epc") String epc,
    		@RequestParam("brandid") int brandid,@RequestParam("modelid") int modelid,
    		@RequestParam("patternid") int patternid,@RequestParam("depth") int depth,
    		@RequestParam("type") String type,@RequestParam("isput") int isput,
    		@RequestParam("vehicleid") String vehicleid,@RequestParam("wheelaliasid") String wheelaliasid){
        TiresBean info = new TiresBean();
        info.setBrandid(brandid);
        info.setCreated(new Date());
        info.setDepth(depth);
        info.setEpc(epc);
        info.setIsput(isput);
        info.setLastmodified(new Date());
        info.setMiles(miles);
        info.setModelid(modelid);
        info.setPatternid(patternid);
        info.setType(type);
        if(vehicleid!=null&&!"".equals(vehicleid)){
        	int a = Integer.valueOf(vehicleid);
        	info.setVehicleid(a);
        }
        if(wheelaliasid!=null&&!"".equals(wheelaliasid)){
        	int a = Integer.valueOf(wheelaliasid);
        	info.setWheelaliasid(a);
        }
    	JSONObject result = new JSONObject();
        result.put("success",carsManageService.bindingTire(info));
        result.put("msg","");
        return result.toJSONString();
    }




    /**
     * 轮胎检查
     */
    @RequestMapping(value = "/tireCheck",method = RequestMethod.GET,produces = "application/json; charset=gbk")
    @ResponseBody
    public String getTireCheck(@RequestParam(value = "tireid",required = true)int tireid,@RequestParam(value = "depth",required = true)int depth,
                               @RequestParam(value = "step",required = true)int step,@RequestParam(value = "createid",required = true)int createid,
                               @RequestParam(value = "memo",required = true)String memo,@RequestParam(value = "reasonid",required = true)int reasonid,
                               @RequestParam(value = "branchid",required = true)int branchid)    {
        JSONObject result = new JSONObject();

        result.put("success",carsManageService.tireCheck(tireid,depth,step,createid,memo,reasonid,branchid));
        result.put("msg","");
        return result.toJSONString();
    }
//    根据异常部位获取异常原因
    @RequestMapping(value = "/findReasonName",method = RequestMethod.GET,produces = "application/json; charset=gbk")
    @ResponseBody
    public String findReasonName(){
        JSONObject result = new JSONObject();
        result.put("success",true);
        result.put("msg","");
        result.put("data",carsManageService.findReasonName());
        return JSONObject.toJSONString(result,serializeConfig);
    }
    /**
     * 在车检查的轮位操作
    */
    @RequestMapping(value = "/carsTiresCheck" ,method = RequestMethod.GET,produces = "application/json; charset=gbk" )
    @ResponseBody
    public String carsTiresCheck(@RequestParam(value = "id",required = true)int id,@RequestParam(value = "depth",required = true)int depth){
        JSONObject result = new JSONObject();
        result.put("success",carsManageService.carsTirescheck(id,depth));
        result.put("msg","");
        return result.toJSONString();
    }


    /**
     * 轮胎装车
     * @param vehicleepc
     * @param installepc
     * @param unloadepc
     * @param wheelid
     * @param creatorid
     * @param reasonid
     * @param branchid
     * @param stockid
     * @return
     */
    @RequestMapping(value = "/changeTire",method = RequestMethod.POST, produces = "application/json; charset=gbk")
    @ResponseBody
    public String changeTire(@RequestParam("vehicleepc")String vehicleepc,@RequestParam("installepc")String installepc,
                             @RequestParam("unloadepc")String unloadepc,@RequestParam("wheelid")int wheelid,
                             @RequestParam("creatorid")int creatorid,@RequestParam("reasonid")int reasonid,
                             @RequestParam("branchid")int branchid,@RequestParam("stockid")int stockid){
        int vehicleid = carsManageService.getVehicleIdByEpc(vehicleepc);
        int installid = carsManageService.getInstallIdByEpc(installepc);
        int unloadid = carsManageService.getUnloadIdByEpc(unloadepc);
        TireEventsBean install = new TireEventsBean();
        install.setVehicleid(vehicleid);
        install.setTireid(installid);
        install.setWheelaliasid(wheelid);
        install.setCreatorid(creatorid);
        install.setStockid(stockid);
        install.setReasonid(reasonid);
        install.setBranchid(branchid);
        TireEventsBean unload = new TireEventsBean();
        unload.setVehicleid(vehicleid);
        unload.setTireid(unloadid);
        unload.setWheelaliasid(wheelid);
        unload.setCreatorid(creatorid);
        unload.setStock1id(stockid);
        unload.setReasonid(reasonid);
        unload.setBranchid(branchid);
        JSONObject result = new JSONObject();
        result.put("success",carsManageService.updateTire(installid,unloadid,vehicleid,wheelid,stockid,install,unload));
        result.put("msg","");
        return result.toJSONString();
    }

    /**
     * 轮胎换位
     * @param tire1epc
     * @param tire2epc
     * @param vehicleepc
     * @param wheel1id
     * @param wheel2id
     * @param creatorid
     * @param reasonid
     * @param branchid
     * @return
     */

    @RequestMapping(value = "/exchangeWheels",method = RequestMethod.POST, produces = "application/json; charset=gbk")
    @ResponseBody
    public String exchangeWheels(@RequestParam("tire1epc")String tire1epc,@RequestParam("tire2epc")String tire2epc,
                                 @RequestParam("vehicleid")String vehicleepc,@RequestParam("wheel1id")int wheel1id,
                                 @RequestParam("wheel2id")int wheel2id,@RequestParam("creatorid")int creatorid,
                                 @RequestParam("reasonid")int reasonid, @RequestParam("branchid")int branchid){
        int vehicleid = carsManageService.getVehicleIdByEpc(vehicleepc);
        int tire1id =carsManageService.getTire1IdByEpc(tire1epc);
        int tire2id =carsManageService.getTire2IdByEpc(tire2epc);
        TireEventsBean install1 = new TireEventsBean();
        install1.setTireid(tire2id);
        install1.setVehicleid(vehicleid);
        install1.setWheelid(wheel1id);
        install1.setCreatorid(creatorid);
        install1.setBranchid(branchid);
        TireEventsBean unload1 = new TireEventsBean();
        unload1.setTireid(tire1id);
        unload1.setVehicleid(vehicleid);
        unload1.setWheelid(wheel1id);
        unload1.setCreatorid(creatorid);
        unload1.setBranchid(branchid);
        TireEventsBean install2 = new TireEventsBean();
        install2.setTireid(tire1id);
        install2.setVehicleid(vehicleid);
        install2.setWheelid(wheel2id);
        install2.setCreatorid(creatorid);
        install2.setBranchid(branchid);
        TireEventsBean unload2 = new TireEventsBean();
        unload2.setTireid(tire2id);
        unload2.setVehicleid(vehicleid);
        unload2.setWheelid(wheel2id);
        unload2.setCreatorid(creatorid);
        unload2.setBranchid(branchid);
        JSONObject result=new JSONObject();
        result.put("success",carsManageService.exchangeWheels(tire1id,  vehicleid,
                wheel2id, tire2id, wheel1id,
                install1,unload1, install2,unload2));
        result.put("msg","");
        return result.toJSONString();
    }


}
